cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(cu_proj)

option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
set(CMAKE_CXX_STANDARD 17)
add_definitions(-std=c++17)
set(CMAKE_BUILD_TYPE Release)#Debug#Release
#set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/workspace)
#set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/workspace)

find_package(CUDA REQUIRED)
find_package(OpenCV)

include_directories(
    ${CUDA_INCLUDE_DIRS}
    ${OpenCV_INCLUDE_DIRS}
)

message(${CUDA_INCLUDE_DIRS})
message(${CUDA_LIBRARIES})

# 如果你是不同显卡，请设置为显卡对应的号码参考这里：https://developer.nvidia.com/zh-cn/cuda-gpus#compute
if ("${GPU_CC}" STREQUAL "")
set(GPU_CC 89)
endif()

set(CUDA_GEN_CODE "-gencode=arch=compute_${GPU_CC},code=sm_${GPU_CC}")
#debug
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -O0 -g -Wfatal-errors -pthread -w")
# set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++17 -O0 -g -G -Xcompiler -fPIC -w ${CUDA_GEN_CODE}")
#release
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -O3 -Wfatal-errors -pthread -w")
# set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++17 -O3 -Xcompiler -fPIC -w ${CUDA_GEN_CODE}")
# too many resources requested for launch 的错误，见 https://zhuanlan.zhihu.com/p/705797869
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++17 -O3 -Xcompiler -fPIC -w --ptxas-options=-v ${CUDA_GEN_CODE}")
# set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++17 -O3 -Xcompiler -fPIC -w -maxrregcount=64 --ptxas-options=-v ${CUDA_GEN_CODE}")
# set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++17 -O3 -Xcompiler -fPIC -w -maxrregcount=64 ${CUDA_GEN_CODE}")

include_directories(${PROJECT_SOURCE_DIR}/include)

#kernel
include_directories(${PROJECT_SOURCE_DIR}/kernel)
file(GLOB_RECURSE cu_files1 ${PROJECT_SOURCE_DIR}/kernel/*.cu)
set(kernelCuSo "kernelCu")
cuda_add_library(${kernelCuSo} ${cu_files1})
target_link_libraries(${kernelCuSo} ${CUDA_LIBRARIES} cuda cublas)
#target_link_libraries(${kernelCuSo} ${CUDA_LIBRARIES} cuda ${CUDA_cublas_LIBRARIES})

#proj
set(project1 cu_exe)
FILE(GLOB_RECURSE CPP_FILE0 ${PROJECT_SOURCE_DIR}/src/*.cpp)
add_executable(${project1} ${CPP_FILE0})
target_link_libraries(${project1} ${kernelCuSo} ${OpenCV_LIBS})
